package com.example.ai.etl.reader;

import org.springframework.ai.document.Document;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;

import java.net.MalformedURLException;
import java.util.List;

@Component
public class MyTikaDocumentReader {


    public List<Document> loadText(String classPathFile) {
        TikaDocumentReader tikaDocumentReader = new TikaDocumentReader("classpath:/"+classPathFile);
        return tikaDocumentReader.read();
    }
    public List<Document> loadUrl(String url){
        try {
            UrlResource urlResource = new UrlResource(url);
            TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(urlResource);
            List<Document> documents = tikaDocumentReader.read();
            return  documents;
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        MyTikaDocumentReader reader = new MyTikaDocumentReader();
        /*List<Document> documents = reader.loadText("浙江省劳动人事争议仲裁申请书V1.1.docx");
        System.out.println(documents);*/
        List<Document> documents = reader.loadUrl("https://docs.spring.io/spring-ai/reference/1.0/index.html");
        System.out.println(documents);

    }
}